/*
 * All rights Reserved, Designed By DataDriver
 * Copyright:    DataDriver.Inc
 * Company:      Zhuo Wo Infomation Technology (ShangHai) CO.LTD
 */
package com.sun.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {

    @Pointcut("execution(* com.sun.aop.MathCalculater.div(..))")
    public void aopMethod() {
    }

    @Before("aopMethod()")
    public void logStart() {
        System.out.println("除法操作开始了....");
    }

    @After("aopMethod()")
    public void logEnd() {
        System.out.println("除法操作结束了....");
    }

    @AfterReturning(value = "aopMethod()", returning = "returnValue")
    public void logReturn(JoinPoint joinPoint, Object returnValue) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("参数值为:" + arg);
        }
        System.out.println("除法操作返回了....: {}" + returnValue);
    }

    /**
     * JoinPoint一定要放在参数表的第一位
     * @param joinPoint
     */
    @AfterThrowing(value = "aopMethod()",throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception) {
        String name = joinPoint.getSignature().getName();
        System.out.println("方法"+name+"操作出现异常....: {"+exception+"}");
    }
}
